#define PORT 6666
#define MAXSIZE 1024
#include <iostream>
#include <cstring>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <cstdio>

using namespace std;

int main(int argc,char* argv[])
{
    char buff[1024];
    char name[1024];
    //if(argc<2)
    // {
    //     cout<<"please use it by ./proname name"<<endl;
    //    return 0;
    //}
    //strcpy(name,argv[1]);
	int client_fd;
	int rtv=0;
    struct sockaddr_in my_addr;
	rtv=client_fd = socket(AF_INET, SOCK_STREAM, 0);
    if( rtv == -1) {
        cout<<"socket create error."<<endl;
        return 1;
    }
	else
	{
		cout<<"socket create success!"<<endl;
	}
    my_addr.sin_family=AF_INET;
    my_addr.sin_port=htons(PORT);
    my_addr.sin_addr.s_addr= inet_addr("127.0.0.1");
	rtv=connect(client_fd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));
    if(rtv == -1) {
        cout<<"connect error."<<endl;
        return 1;
    }
	else
	{
		cout<<"connect success!"<<endl;
	}
	while(strcmp(name,"quit"))
	{
		scanf("%s",&name);
		if(!strcmp(name,"quit"))
            break;
  		  rtv=send(client_fd, &name, MAXSIZE, 0);
  	  if( rtv == -1)
 	   {
  	      cout<<"send failed."<<endl;
 	   }
  	  rtv=recv(client_fd, &buff, MAXSIZE, 0);
  	  if( rtv == -1)
  	  {
  	      cout<<"recv failed."<<endl;
  	  }
  	  cout<<buff<<endl;
	}
  	  close(client_fd);
    return 0;
}
